var DictionaryService = function () {

    var menuService = new Object();

    menuService.saveMenu = function (updateType, $form, successCallback) {
        clearFormValidateMsg($form);
        var pass = validateItemNotNull($form.find("#const_name"));
        if (!pass)
            return;
        var pass = validateItemNotNull($form.find("#const_code"));
        if (!pass)
            return;
        var requestData = {
            const_id:$form.find("#const_id").val(),
            pconst_id:$form.find("#pconst_id").val(),
            const_name:$form.find("#const_name").val(),
            const_code:$form.find("#const_code").val(),
            bflag:$form.find("#bflag").val(),
            pym:$form.find("#pym").val()
        };
        var title = "";
        var text = "";
        if (updateType == "NEW.TOP") {
            requestData.updateType="NEW.TOP";
            title = "确定添加?";
            text = "是否确定添加新的字典!";
        }
        else if (updateType == "NEW.CHILD") {
            requestData.updateType="NEW.CHILD";
            title = "确定添加?";
            text = "是否确定添加新的字典!";
        }
        else if (updateType == "EDIT") {
            requestData.updateType="UPDATE";
            title = "确定修改?";
            text = "是否确定修改此字典!";
        }
        else
            return;

        swal({
            title: title,
            text: text,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (result.value) {
                saveWebMenu(requestData, function (data) {
                    successCallback(updateType, data);
                });
            }
        });
    };

    function getContextPath() {
        var pathName = document.location.pathname;
        var index = pathName.substr(1).indexOf("/");
        var result = pathName.substr(0, index + 1);
        return result;
    }

    function saveWebMenu(requestData, successCallback) {
        var params = {datas: JSON.stringify(requestData)};
        $.ajax({
            type: 'POST',
            data: params,
            url: getContextPath() + '/dictionary/save',
            success: function (data) {
                //成功之后
                if (data.success) {
                    if (successCallback != null)
                        successCallback(data.data);
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    };

    menuService.deleteWebMenu = function (cModelID, successCallback) {
        if (cModelID == null)
            return;

        swal({
            title: "确定删除?",
            text: "确定删除当前字典及其子字典",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (result.value) {
                deleteWebMenu(cModelID, successCallback);
            }
        });
    };


    menuService.syncPermission = function (successCallback) {
        $.ajax({
            type: 'POST',
            url: '/system/dictionary/syncPermission/',
            success: function (data) {
                //成功之后
                if (data.success) {
                    if (successCallback != null)
                        successCallback();
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    };

    function deleteWebMenu(cModelID, successCallback) {
        $.ajax({
            type: 'DELETE',
            url: '/system/dictionary/delete/' + cModelID,
            success: function (data) {
                //成功之后
                if (data.success) {
                    if (successCallback != null)
                        successCallback();
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    };

    menuService.loadMenuDetail = function (menuxh, successCallback, errorCallback) {
        $.ajax({
            type: 'POST',
            url: '/system/dictionary/menudata/' + menuxh,
            success: function (data) {
                if (data.success) {
                    if (successCallback != null) {
                        successCallback(data.data);
                    }
                }
                else {
                    if (errorCallback != null) {
                        errorCallback(data.errorMsg);
                    }
                }
            },
            error: function (xhr, type) {
                if (errorCallback != null) {
                    errorCallback("系统发生内部错误!请稍后再试!");
                }
            }
        });
    }

    function getJson(hrefStr){
        var splitStr = hrefStr.split("&");  // 返回结果：["userName=cyq", "age=24", "sex=f"]
        var urlObj = {}  // 等价于 var urlObj = new Object()
        for(var i = 0; i < splitStr.length; i++){
            urlObj[splitStr[i].split("=")[0]] = splitStr[i].split("=")[1];
        }
        return urlObj;
    }

    return menuService;
}